//
//  SmsCloudHomeController.m
//  smsCloud
//
//  Created by 蓝鳍互娱 on 2024/7/17.
//

#import "SmsCloudHomeController.h"
#import "SmsCloudMainController.h"
#import "SmsCloudSearchController.h"
#import "SmsCloudLongImgController.h"
#import "SmsCloudAearController.h"
#import "SmsCloudPhoneController.h"
#import "SmsCloudRecordController.h"
#import "SmsCloudBannerView.h"
#import "SmsCloudSearchView.h"
#import "SmsCloudEarchView.h"
#import "SmsCloudTableView.h"
#import "SmsCloudVipController.h"
@interface SmsCloudHomeController ()
@property (nonatomic,strong) SmsCloudBannerView *smsCloudBannerView;
@property (nonatomic,strong) UIView *smsCloudBottomView;
@property (nonatomic,strong) UIButton *smsCloudSetBtn;
@property (nonatomic,strong) SmsCloudSearchView *smsCloudSearchView;
@property (nonatomic,strong) SmsCloudEarchView  *smsCloudEarchView;
@property (nonatomic,strong) SmsCloudTableView  *smsCloudTableView;

@property (nonatomic,strong) UIButton *smsCloudFirstUserBtn;
@property (nonatomic,strong) NSArray *smsCloudFirstUsers;
@property (nonatomic,assign) NSInteger smsCloudFirstUserIndex;
@end
@implementation SmsCloudHomeController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [SmsCloudDataHelper requestSmsCloudPrice];
  
    self.smsCloudFirstUsers = @[@"smsCloudGuideFirst",@"smsCloudGuideSecond",@"smsCloudGuideThree"];
    self.smsCloudFirstUserIndex = 0;
    [self smsCloudInitView];
    [self smsCloudRequestProductList];
}
-(UIButton *)smsCloudFirstUserBtn{
    if (!_smsCloudFirstUserBtn) {
        _smsCloudFirstUserBtn = [[UIButton alloc] init];
        _smsCloudFirstUserBtn.userInteractionEnabled = true;
        _smsCloudFirstUserBtn.adjustsImageWhenHighlighted = true;
        [_smsCloudFirstUserBtn addTarget:self action:@selector(smsCloudChangeUser) forControlEvents:UIControlEventTouchUpInside];
    }
    return _smsCloudFirstUserBtn;
}
-(SmsCloudBannerView *)smsCloudBannerView{
    if (!_smsCloudBannerView) {
        __weak __typeof__(self) weakSelf = self;
        _smsCloudBannerView = [[SmsCloudBannerView alloc] initWithFrame:CGRectMake(0, 0, [SmsCloudHead SmsCloudScreewWidth], [SmsCloudHead SmsCloudScreewRatio:510])];
        _smsCloudBannerView.SmsCloudBannerBlock = ^(NSInteger index) {
            SmsCloudLongImgController * smsCloudLongImgCtrl = [[SmsCloudLongImgController alloc] init];
            smsCloudLongImgCtrl.longImgName = index == 0 ? @"smsCloudBannerGo_1":@"smsCloudBannerGo_2";
            [weakSelf.navigationController pushViewController:smsCloudLongImgCtrl animated:true];
        };
    }
    return _smsCloudBannerView;
}
-(UIView *)smsCloudBottomView{
    if (!_smsCloudBottomView) {
        _smsCloudBottomView = [[UIView alloc] init];
        _smsCloudBottomView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#EEF6FC"];
    }
    return _smsCloudBottomView;
}
-(UIButton *)smsCloudSetBtn{
    if (!_smsCloudSetBtn) {
        _smsCloudSetBtn = [[UIButton alloc] init];
        [_smsCloudSetBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudHomeSet"] forState:UIControlStateNormal];
        [_smsCloudSetBtn addTarget:self action:@selector(smsCloudSet) forControlEvents:UIControlEventTouchUpInside];
    }
    return _smsCloudSetBtn;
}
-(SmsCloudSearchView *)smsCloudSearchView{
    if (!_smsCloudSearchView) {
        _smsCloudSearchView = [[SmsCloudSearchView alloc] init];
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchView.SmsCloudSearchBlock = ^{
            if ([SmsCloudHead isSmsCloudVip]) {
                SmsCloudSearchController * smsCloudSearchCtrl = [[SmsCloudSearchController alloc] init];
                [weakSelf.navigationController pushViewController:smsCloudSearchCtrl animated:true];
            }else{
                SmsCloudVipController * smsCloudVipCtrl = [[SmsCloudVipController alloc] init];
                UINavigationController * smsCloudVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:smsCloudVipCtrl];
                smsCloudVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakSelf presentViewController:smsCloudVipNavCtrl animated:true completion:^{}];
            }
        };
    }
    return _smsCloudSearchView;
}
-(SmsCloudEarchView *)smsCloudEarchView{
    if (!_smsCloudEarchView) {
        __weak __typeof__(self) weakSelf = self;
        _smsCloudEarchView = [[SmsCloudEarchView alloc] init];
        _smsCloudEarchView.SmsCloudEarchBlock = ^{
            if ([SmsCloudHead isSmsCloudVip]) {
                SmsCloudAearController * smsCloudAearCtrl = [[SmsCloudAearController alloc] init];
                [weakSelf.navigationController pushViewController:smsCloudAearCtrl animated:true];
            }else{
                SmsCloudVipController * smsCloudVipCtrl = [[SmsCloudVipController alloc] init];
                UINavigationController * smsCloudVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:smsCloudVipCtrl];
                smsCloudVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakSelf presentViewController:smsCloudVipNavCtrl animated:true completion:^{}];
            }
        };
        _smsCloudEarchView.SmsCloudRecordBlock = ^{
            if ([SmsCloudHead isSmsCloudVip]) {
                SmsCloudRecordController * smsCloudRecordCtrl = [[SmsCloudRecordController alloc] init];
                [weakSelf.navigationController pushViewController:smsCloudRecordCtrl animated:true];
            }else{
                SmsCloudVipController * smsCloudVipCtrl = [[SmsCloudVipController alloc] init];
                UINavigationController * smsCloudVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:smsCloudVipCtrl];
                smsCloudVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakSelf presentViewController:smsCloudVipNavCtrl animated:true completion:^{}];
            }
        };
    }
    return _smsCloudEarchView;
}
-(SmsCloudTableView *)smsCloudTableView{
    if (!_smsCloudTableView) {
        __weak __typeof__(self) weakSelf = self;
        _smsCloudTableView = [[SmsCloudTableView alloc] init];
        _smsCloudTableView.SmsCloudProductBlock = ^(SmsCloudProductModel * _Nonnull smsCloudProductMdl) {
            if ([SmsCloudHead isSmsCloudVip]) {
                SmsCloudPhoneController *smsCloudPhoneCtrl = [[SmsCloudPhoneController alloc] init];
                smsCloudPhoneCtrl.smsCloudProductMdl = smsCloudProductMdl;
                [weakSelf.navigationController pushViewController:smsCloudPhoneCtrl animated:true];
            }else{
                SmsCloudVipController * smsCloudVipCtrl = [[SmsCloudVipController alloc] init];
                UINavigationController * smsCloudVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:smsCloudVipCtrl];
                smsCloudVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakSelf presentViewController:smsCloudVipNavCtrl animated:true completion:^{}];
            }
        };
    }
    return _smsCloudTableView;
}
- (void)smsCloudInitView{
    [self.view addSubview:self.smsCloudBannerView];
    [self.view addSubview:self.smsCloudBottomView];
    [self.view addSubview:self.smsCloudSetBtn];
    [self.view addSubview:self.smsCloudSearchView];
    [self.view addSubview:self.smsCloudEarchView];
    [self.view addSubview:self.smsCloudTableView];

    [self.smsCloudBannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:510]));
    }];
    [self.smsCloudBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:311] + [SmsCloudHead SmsCloudTopSafeH]));
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
    [self.smsCloudSetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:17] + [SmsCloudHead SmsCloudTopSafeH]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:24]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
    }];
    [self.smsCloudSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:45]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:45]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:254.33] + [SmsCloudHead SmsCloudTopSafeH]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:89]));
    }];
    [self.smsCloudEarchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:12]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:12]));
        make.top.equalTo(self.smsCloudSearchView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:28.6]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:196]));
    }];
    [self.smsCloudTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudEarchView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:25]);
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
    NSString *smsCloudFirst = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"smsCloudFirst"]];
    if((smsCloudFirst.length > 0)&&[smsCloudFirst isEqualToString:@"2"]){
        [self performSelector:@selector(showVip) withObject:self afterDelay:1.0];
    }else{
        [self.view addSubview:self.smsCloudFirstUserBtn];
        [self.smsCloudFirstUserBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        }];
        [self.smsCloudFirstUserBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:self.smsCloudFirstUsers[self.smsCloudFirstUserIndex]] forState:UIControlStateNormal];
    }
}
- (void)smsCloudSet{
    SmsCloudMainController * smsCloudMainCtrl = [[SmsCloudMainController alloc] init];
    [self.navigationController pushViewController:smsCloudMainCtrl animated:true];
}
- (void)smsCloudRequestProductList{
    __weak __typeof__(self) weakSelf = self;
    [SmsCloudDataHelper requestSmsCloudProductList:^{
        weakSelf.smsCloudTableView.smsClouds = [SmsCloudDataHelper defaultMag].smsCloudProducts;
    }];
}
- (void)smsCloudChangeUser{
    self.smsCloudFirstUserIndex ++;
    if (self.smsCloudFirstUserIndex < 3) {
        [self.smsCloudFirstUserBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:self.smsCloudFirstUsers[self.smsCloudFirstUserIndex]] forState:UIControlStateNormal];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"smsCloudFirst"];
        [self.smsCloudFirstUserBtn removeFromSuperview];
        [self showVip];
    }
}
- (void)showVip{
    if(![SmsCloudHead isSmsCloudVip]){
        SmsCloudVipController * smsCloudVipCtrl = [[SmsCloudVipController alloc] init];
        UINavigationController * smsCloudVipNavCtrl = [[UINavigationController alloc] initWithRootViewController:smsCloudVipCtrl];
        smsCloudVipNavCtrl.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:smsCloudVipNavCtrl animated:true completion:^{}];
    }
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    [self roundSize:[SmsCloudHead SmsCloudScreewRatio:40] andCorner:UIRectCornerTopLeft|UIRectCornerTopRight];
}
-(void)roundSize:(CGFloat)round andCorner:(UIRectCorner)corners{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, [SmsCloudHead SmsCloudScreewWidth], [SmsCloudHead SmsCloudScreewHight] - ([SmsCloudHead SmsCloudScreewRatio:311] + [SmsCloudHead SmsCloudTopSafeH])) byRoundingCorners:corners cornerRadii:CGSizeMake(round, round)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, [SmsCloudHead SmsCloudScreewWidth], [SmsCloudHead SmsCloudScreewHight] - ([SmsCloudHead SmsCloudScreewRatio:311] + [SmsCloudHead SmsCloudTopSafeH]));
    maskLayer.path = maskPath.CGPath;
    
    CAShapeLayer *borderMaskLayer = [CAShapeLayer layer];
    borderMaskLayer.path = maskPath.CGPath;
    borderMaskLayer.lineWidth = self.smsCloudBottomView.layer.borderWidth;
    borderMaskLayer.strokeColor = self.smsCloudBottomView.layer.borderColor;
    borderMaskLayer.fillColor = [UIColor clearColor].CGColor;
    
    self.smsCloudBottomView.layer.mask = maskLayer;
    [self.smsCloudBottomView.layer addSublayer:borderMaskLayer];
}
@end
